// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import Router from 'vue-router'
import routes from './configs/router.js'
import 'element-ui/lib/theme-default/index.css'
import '../static/css/custom-elementui.css'
Vue.use(ElementUI)

Vue.use(Router)
Vue.config.productionTip = false

import store from './store/';
import './configs/api-register.js';


const router = new Router({
    routes
});




router.beforeEach((to, from, next) => {
    window.scroll(0, 0);
    var userInfo = store.state.user.userInfo;
    if (to.path === '/login') {
        next();
    } else {
        if (userInfo.token) {
            var web_routes = typeof userInfo.web_routes === 'object' ? userInfo.web_routes : [];
            //判断当前用户是否拥有目标页面访问权限，防止用户强制在地址栏输入访问没有权限的页面
            if (!web_routes.includes(to.path)) {
                ElementUI.MessageBox.alert('您没用权限访问此页面或者输入地址有误！如果您的权限刚刚被提升，请退出后重新登录系统', '错误', {
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(() => {
                    if (from.path) {
                        next(from.path)
                    } else if (userInfo.default_web_route) {
                        next(userInfo.default_web_route);
                    } else {
                        localStorage.removeItem('ms_username')
                        store.dispatch("set_userInfo", null)
                        next('/login');
                    }
                });
            } else {
                next();
            }
        } else {
            next('/login');
        }

    }
})



new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')